/*
 * ***********************************************
 * 简易太空人时钟代码 
 * ***********************************************
 * 程序硬件介绍 esp8266 nodemcu + LCD屏幕240×320（ST7789）
 * 
 * 必读！！！
 * ---------------------------
 * 开发板要改成 esp8266的（版本2.7.4及以下编译正常） 
 * 否则编译报错或不通过，其他：如果代码已经烧录了，屏幕亮但是全屏空白有两种思路解决：
 * 1、检查接线，接线没错用第二种方法
 * 2、打开库文件目录：Arduino\libraries\TFT_eSPI\User_Setup.h在这修改：
 * (esp32在193到197行，esp8266在157到159行，用esp32时不需注释掉esp8266的，但用esp8266时要注释掉esp32的)
 *  #define TFT_CS   PIN_D8  // Chip select control pin D8
    #define TFT_DC   PIN_D3  // Data Command control pin
    #define TFT_RST  PIN_D4  // Reset pin (could connect to NodeMCU RST, see next line)
 * 把那些注释去掉保存
 * ---------------------------
 * 必读！！！
 * 
 * 具体接线对应如下：
 * TFT屏幕      nodemcu         esp82666
 * GND           GND              GND
 * VCC           3V3              3V3
 * SCK           D5              GPIO14
 * SDA           D7              GPIO13
 * RST           D4              GPIO2
 * DC            D3              GPIO0
 * CS            D8              GPIO15
 * BLK           可以不接(控制屏幕背光)
 * 
 * B站原作者视频及资料
 * 视频：https://www.bilibili.com/video/BV1FX4y1g7MF?from=search&seid=12720907928279103025
 * 资料：https://pan.baidu.com/s/1Dsu4VmMSnOTbIbv7zvFAkQ 提取码: cgkk
 * 
 */
#include <ArduinoJson.h>
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ESP8266HTTPClient.h>

//const char ssid[] = "Laboratory-203";  //WIFI名称 修改这2个就可以了
//const char pass[] = "shiyanshi203";  //WIFI密码

const char ssid[] = "OrayBox-2.4G-8030";  //WIFI名称 修改这2个就可以了
const char pass[] = "bt000000";  //WIFI密码

#include "font/ZdyLwFont_20.h"
#include "font/FxLED_32.h"

#include "img/main_img/main_img.h"
#include "img/pangzi/i0.h"
#include "img/pangzi/i1.h"
#include "img/pangzi/i2.h"
#include "img/pangzi/i3.h"
#include "img/pangzi/i4.h"
#include "img/pangzi/i5.h"
#include "img/pangzi/i6.h"
#include "img/pangzi/i7.h"
#include "img/pangzi/i8.h"
#include "img/pangzi/i9.h"
//#include "img/Gif/ziji.h" //杂技
#include "img/Gif/dagu.h" //
#include "img/Gif/zzzzzzz.h"  //睡觉

#include "img/temperature.h"
#include "img/humidity.h"
//#include "img/watch_top.h"
//#include "img/watch_bottom.h"

#include <TFT_eSPI.h> 
#include <SPI.h>

TFT_eSPI tft = TFT_eSPI();  // 引脚请自行配置tft_espi库中的 User_Setup.h文件
TFT_eSprite clk = TFT_eSprite(&tft);


#include <TJpg_Decoder.h>

unsigned int Gif_Mode = 1;  //动画
int prevTime = 0;
uint32_t targetTime = 0;   
byte omm = 99;
boolean initial = 1;
byte xcolon = 0;
unsigned int colour = 0;

uint16_t bgColor = 0xFFFF;
//String cityCode = "101040100";  //天气城市代码
//String cityCode = "101300301";  //柳州
String cityCode = "101300903";  //北流

// String cityCode = "101280601";  //深圳

//NTP服务器
static const char ntpServerName[] = "ntp6.aliyun.com";
const int timeZone = 8;             //东八区


WiFiUDP Udp;
unsigned int localPort = 8000;

time_t getNtpTime();
void digitalClockDisplay();
void printDigits(int digits);
String num2str(int digits);
void sendNTPpacket(IPAddress &address);


bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t* bitmap)
{
  if ( y >= tft.height() ) return 0;
  tft.pushImage(x, y, w, h, bitmap);
  // Return 1 to decode next block
  return 1;
}

byte loadNum = 6;
void loading(byte delayTime){
  clk.setColorDepth(8);
  
  clk.createSprite(200, 50);
  clk.fillSprite(0x0000);

  clk.drawRoundRect(0,0,200,16,8,0xFFFF);
  clk.fillRoundRect(3,3,loadNum,10,5,0xFFFF);
  clk.setTextDatum(CC_DATUM);
  clk.setTextColor(TFT_GREEN, 0x0000); 
  clk.drawString("Connecting to WiFi",100,40,2);
  clk.pushSprite(60,240);
  clk.deleteSprite();
  loadNum += 1;
  if(loadNum>=194){
    loadNum = 194;
  }
  delay(delayTime);
}

void setup()
{
  Serial.begin(9600);
  
  tft.init();
  tft.setRotation(0);
  tft.fillScreen(0x0000);
  tft.setTextColor(TFT_BLACK, bgColor);
  
  targetTime = millis() + 1000; 

  Serial.print("正在连接WIFI ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    for(byte n=0;n<10;n++){ //每500毫秒检测一次状态
      loading(50);
    }
  }
  while(loadNum < 194){ //让动画走完
    loading(1);
  }

  Serial.print("本地IP： ");
  Serial.println(WiFi.localIP());
  //Serial.println("启动UDP");
  Udp.begin(localPort);
  //Serial.print("端口号: ");
  //Serial.println(Udp.localPort());
  //Serial.println("等待同步...");
  setSyncProvider(getNtpTime);
  setSyncInterval(300);

  
  TJpgDec.setJpgScale(1);
  TJpgDec.setSwapBytes(true);
  TJpgDec.setCallback(tft_output);

//  TJpgDec.drawJpg(0,0,watchtop, sizeof(watchtop));
//  TJpgDec.drawJpg(0,220,watchbottom, sizeof(watchbottom));

  
  //绘制一个视口
  tft.setViewport(0, 0, 320,480);
  tft.fillScreen(0x0000);
  tft.fillRoundRect(0,0,320,480,0,bgColor);//实心圆角矩形
  //tft.resetViewport();

  //绘制线框
  tft.drawFastHLine(0,34,320,TFT_BLACK);  //横线
  
  tft.drawFastVLine(150,0,34,TFT_BLACK);
  
  tft.drawFastHLine(0,166,320,TFT_BLACK);
  
  tft.drawFastVLine(60,166,34,TFT_BLACK); //竖线
  tft.drawFastVLine(160,166,34,TFT_BLACK);
  tft.drawFastHLine(0,200,320,TFT_BLACK);
  tft.drawFastHLine(0,241,320,TFT_BLACK);
  
  getCityCode();  //获取城市代码
  
  TJpgDec.drawJpg(161,171,temperature, sizeof(temperature));  //温度图标
  TJpgDec.drawJpg(159,130,humidity, sizeof(humidity));  //湿度图标
  
}

time_t prevDisplay = 0; // 显示时间
unsigned long weaterTime = 0;

void loop(){
/*
  if (now() != prevDisplay) {
    prevDisplay = now();
    digitalClockDisplay();

  }*/
    if (timeStatus() != timeNotSet) {
    if (now() != prevDisplay) { //update the display only if time has changed
      prevDisplay = now();  
      digitalClockDisplay();
    }
  }
  
  if(millis() - weaterTime > 300000){ //5分钟更新一次天气
    weaterTime = millis();
    getCityWeater();
  }

  scrollBanner();
  ButtonscrollBanner();
//  imgAnim();
  imgDisplay();
}

// 发送HTTP请求并且将服务器响应通过串口输出
void getCityCode(){
  int OldConnectionTimes = millis(),NewConnectionTimes = 0;
  //创建 HTTPClient 对象
  HTTPClient httpClient;
    String URL = "http://wgeo.weather.com.cn/ip/?_="+String(now());
    
    //配置请求地址。此处也可以不使用端口号和PATH而单纯的
    httpClient.begin(URL); 
    
    //设置请求头中的User-Agent
    httpClient.setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
    httpClient.addHeader("Referer", "http://www.weather.com.cn/");
  
    //启动连接并发送HTTP请求
    int httpCode = httpClient.GET();
    //Serial.print("Send GET request to URL: ");
    //Serial.println(URL);
    Serial.println("数据请求中...");
    
    //如果服务器响应OK则从服务器获取响应体信息并通过串口输出
    if (httpCode == HTTP_CODE_OK) {
      String str = httpClient.getString();
      //Serial.println(str); 
      int aa = str.indexOf("id=");
      if(aa>-1){
        //cityCode = str.substring(aa+4,aa+4+9);     //注释掉，不然天气默认地址为北京
        //Serial.println(cityCode); 
        Serial.println("获取城市代码成功"); 
        getCityWeater();
      }else{
        Serial.println("获取城市代码失败，正在重新获取...");  
      }
    } else {
      Serial.print("请求城市代码错误：");
      Serial.println(String(httpCode) + "正在重新获取...");
    }
    //连接时长超过5秒，直接重启重新连接
    NewConnectionTimes = millis();
    if((NewConnectionTimes - OldConnectionTimes) >= 5000) {
      ESP.restart(); //重启ESP32 
    }
  //关闭ESP8266与服务器连接
  httpClient.end();
}

// 获取城市天气
bool warn_2 = false;
int Warn_Number1 = 0,Warn_Value1 = 0,Warn_Number2 = 0,Warn_Value2 = 0,Warn_Flag = 1;
void getCityWeater(){
  int OldConnectionTimes = millis(),NewConnectionTimes = 0;
  HTTPClient httpClient;
    String URL = "http://d1.weather.com.cn/weather_index/" + cityCode + ".html?_="+String(now());
    //创建 HTTPClient 对象
    
    httpClient.begin(URL); 
    
    //设置请求头中的User-Agent
    httpClient.setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
    httpClient.addHeader("Referer", "http://www.weather.com.cn/");
  
    //启动连接并发送HTTP请求
    int httpCode = httpClient.GET();
    Serial.println("正在获取天气数据");
    Serial.println(URL);

    //如果服务器响应OK则从服务器获取响应体信息并通过串口输出
    if (httpCode == HTTP_CODE_OK) {

      String str = httpClient.getString();
      //Serial.println(str);

      int indexStart = str.indexOf("weatherinfo\":");
      int indexEnd = str.indexOf("};var alarmDZ");
      String jsonCityDZ = str.substring(indexStart+13,indexEnd);
      //Serial.println(jsonCityDZ);

    //气象预警不同时间会发布不同的预警信息，只会显示最新的一个，显示多个也只是显示最新时间的前一个预警，没必要了
      indexStart = str.indexOf("alarmDZ ={\"w\":[");
      indexEnd = str.indexOf("]};var dataSK");
      String jsonDataWarn1 = str.substring(indexStart+15,indexEnd);
      //Serial.println("1="+jsonDataWarn1);
      if(jsonDataWarn1.length() >= 40) {
        Warn_Flag = 1;
      }
      else {
        Warn_Flag = 0;
      }

      indexStart = str.indexOf("dataSK =");
      indexEnd = str.indexOf(";var dataZS");
      String jsonDataSK = str.substring(indexStart+8,indexEnd);
      //Serial.println(jsonDataSK);

      indexStart = str.indexOf("\"f\":[");
      indexEnd = str.indexOf(",{\"fa");
      String jsonFC = str.substring(indexStart+5,indexEnd);
      //Serial.println(jsonFC);

      indexStart = str.indexOf(";var dataZS ={\"zs\":");
      indexEnd = str.indexOf(",\"cn\":\"长沙\"};var fc =");
      String jsonSuggest = str.substring(indexStart+19,indexEnd);
      //Serial.println(jsonSuggest);
      
      weaterData(&jsonCityDZ,&jsonDataSK,&jsonFC,&jsonSuggest,&jsonDataWarn1);
      Serial.println("天气数据获取成功");
    } else {
      Serial.print("请求城市天气错误：");
      Serial.println(String(httpCode) + " 正在重新获取...");
    }
    //连接时长超过5秒，直接重启重新连接
    NewConnectionTimes = millis();
    if((NewConnectionTimes - OldConnectionTimes) >= 3000) {
      //ESP.restart(); //重启ESP32 
    }

  //关闭ESP8266与服务器连接
  httpClient.end();
}

void getLunarCalendar() {
  HTTPClient httpClient;
  String URL = "https://api.xlongwei.com/service/datetime/convert.json";
    //创建 HTTPClient 对象
    
    httpClient.begin(URL); 
    
    //设置请求头中的User-Agent
    httpClient.setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
    httpClient.addHeader("Referer", "http://www.weather.com.cn/");
  
    //启动连接并发送HTTP请求
    int httpCode = httpClient.GET();
    Serial.println("正在获取天气数据");
    Serial.println(URL);

    //如果服务器响应OK则从服务器获取响应体信息并通过串口输出
    if (httpCode == HTTP_CODE_OK) {
      String str = httpClient.getString();
      Serial.println("农历"+str);
    }
}

String scrollText[6];
String ButtonScrollText[8];
//int scrollTextWidth = 0;
//天气信息写到屏幕上
void weaterData(String *cityDZ,String *dataSK,String *dataFC,String *dataSuggest,String *dataWarn1){
  
  DynamicJsonDocument doc(8192);
  deserializeJson(doc, *dataSK);
  JsonObject sk = doc.as<JsonObject>();

  //TFT_eSprite clkb = TFT_eSprite(&tft);
  
  /***绘制相关文字***/
  clk.setColorDepth(8);
  clk.loadFont(ZdyLwFont_20); //加载font/ZdyLwFont_20字体
  //温度
  clk.createSprite(54, 32); 
  clk.fillSprite(bgColor);
  clk.setTextDatum(CC_DATUM);
  clk.setTextColor(TFT_BLACK, bgColor); 
  clk.drawString(sk["temp"].as<String>()+"℃",27,16);
  clk.pushSprite(185,168);
  clk.deleteSprite();
  
  //湿度
  clk.createSprite(56, 24); 
  clk.fillSprite(bgColor);
  clk.setTextDatum(CC_DATUM);
  clk.setTextColor(TFT_BLACK, bgColor); 
  clk.drawString(sk["SD"].as<String>(),28,13);
  //clk.drawString("100%",28,13);
  clk.pushSprite(180,130);
  clk.deleteSprite();

 
  //城市名称
  clk.createSprite(88, 32);  //88,32
  clk.fillSprite(bgColor);
  clk.setTextDatum(CC_DATUM);
  clk.setTextColor(TFT_BLACK, bgColor); 
  //clk.drawString(sk["cityname"].as<String>()+"区",44,18);
  clk.drawString(sk["cityname"].as<String>(),44,18);
  clk.pushSprite(151,1);
  clk.deleteSprite();
  
  //PM2.5空气指数
  uint16_t pm25BgColor;//优
  String aqiTxt;
  int pm25V = sk["aqi"];
  //Serial.println("pm25V:" + String(pm25V));
  if(pm25V >= 301) {
    pm25BgColor = tft.color565(255,36,0);//重度
    aqiTxt = "严重";
  }else if(pm25V >= 201 & pm25V <= 300){
    pm25BgColor = tft.color565(136,11,32);//重度
    aqiTxt = "重度";
  }else if(pm25V >= 151 & pm25V <= 200){
    pm25BgColor = tft.color565(186,55,121);//中度
    aqiTxt = "中度";
  }else if(pm25V >= 101 & pm25V <= 160){
    pm25BgColor = tft.color565(242,159,57);//轻
    aqiTxt = "轻度";
  }else if(pm25V >= 51 & pm25V <= 100){
    pm25BgColor = tft.color565(247,219,100);//良
    aqiTxt = "良";
  }else if(pm25V >= 0 & pm25V <= 50) {
    pm25BgColor = tft.color565(156,202,127);//优
    aqiTxt = "优";
  }
  clk.createSprite(50, 24); 
  clk.fillSprite(bgColor);
  clk.fillRoundRect(0,0,50,24,4,pm25BgColor);
  clk.setTextDatum(CC_DATUM);
  clk.setTextColor(0xFFFF); 
  clk.drawString(aqiTxt,25,14);
  clk.pushSprite(5,140);
  clk.deleteSprite();

  //左上角滚动字幕
  //解析第二段JSON
  scrollText[0] = "实时天气 "+sk["weather"].as<String>();
  scrollText[1] = "空气质量 "+aqiTxt;
  
  scrollText[2] = "风向 "+sk["WD"].as<String>()+sk["WS"].as<String>();
  
  deserializeJson(doc, *cityDZ);
  JsonObject dz = doc.as<JsonObject>();
  scrollText[3] = "今日 "+dz["weather"].as<String>();

  //显示天气图标
  String weatherCodeText = dz["weathercode"].as<String>();
 
  
  deserializeJson(doc, *dataFC);
  JsonObject fc = doc.as<JsonObject>();
  
  scrollText[4] = "最低温度 "+fc["fd"].as<String>()+"℃";
  scrollText[5] = "最高温度 "+fc["fc"].as<String>()+"℃";

  //scrollText[6] = "PM2.5 "+sk["aqi"].as<String>();
  
  //Serial.println(scrollText[0]);
  clk.unloadFont(); //释放加载字体资源

  deserializeJson(doc, *dataSuggest);
  JsonObject dataSuggestJson = doc.as<JsonObject>();
  ButtonScrollText[0] = dataSuggestJson["lk_name"].as<String>() + " " + dataSuggestJson["lk_hint"].as<String>();
  ButtonScrollText[1] = dataSuggestJson["cl_name"].as<String>() + " " + dataSuggestJson["cl_hint"].as<String>();
  ButtonScrollText[2] = dataSuggestJson["uv_name"].as<String>() + " " + dataSuggestJson["uv_hint"].as<String>();
  ButtonScrollText[3] = dataSuggestJson["ct_name"].as<String>() + " " + dataSuggestJson["ct_hint"].as<String>();
  ButtonScrollText[4] = dataSuggestJson["gm_name"].as<String>() + " " + dataSuggestJson["gm_hint"].as<String>();
  ButtonScrollText[5] = dataSuggestJson["ys_name"].as<String>() + " " + dataSuggestJson["ys_hint"].as<String>();
  //ButtonScrollText[6] = dataSuggestJson["gz_name"].as<String>() + " " + dataSuggestJson["gz_hint"].as<String>();
  //ButtonScrollText[6] = dataSuggestJson["cl_name"].as<String>() + " " + dataSuggestJson["cl_hint"].as<String>();
  ButtonScrollText[6] = dataSuggestJson["pl_name"].as<String>() + " " + dataSuggestJson["pl_hint"].as<String>();
  ButtonScrollText[7] = dataSuggestJson["co_name"].as<String>() + " " + dataSuggestJson["co_hint"].as<String>();

  deserializeJson(doc, *dataWarn1);
  JsonObject dataWarnjson1 = doc.as<JsonObject>();
  Warn_Number1 = dataWarnjson1["w4"].as<int>();
  Warn_Value1 = dataWarnjson1["w6"].as<int>();
  //Serial.println(dataWarnjson);
  Serial.println("气象预警编号1：" + String(Warn_Number1) + " 等级1：" + String(Warn_Value1));
  /*switch(Warn_Number) { // 处理图片太TM烦躁了，而且显示的效果也特别差，而且预警代码一直没找到文档
    case 7: //高温
      if(Warn_Value == 1) {TJpgDec.drawJpg(175,110,gaowen_lan, sizeof(gaowen_lan));break;}
      if(Warn_Value == 2) {TJpgDec.drawJpg(175,110,gaowen_huang, sizeof(gaowen_huang));break;}
      if(Warn_Value == 3) {TJpgDec.drawJpg(175,110,gaowen_cheng, sizeof(gaowen_cheng));break;}
      if(Warn_Value == 4) {TJpgDec.drawJpg(175,110,gaowen_hong, sizeof(gaowen_hong));break;}
    //case 9: //雷电
    //case 0: //雷暴大风
    //case 2: //暴雨
    default:TJpgDec.drawJpg(175,110,BG_Color, sizeof(BG_Color));Serial.println("NULL");break;
  }*/
  uint16_t weatherWarnBgColor1;
  switch(Warn_Value1) { //这等级把我搞蒙了，一会蓝色是0，一会又变成1
    //填充颜色
    case 1:weatherWarnBgColor1 = tft.color565(0,128,255);break; //蓝色
    case 2:weatherWarnBgColor1 = tft.color565(255,204,51);break; //黄色
    case 3:weatherWarnBgColor1 = tft.color565(255,153,0);break; //橙色
    case 4:weatherWarnBgColor1 = tft.color565(255,0,0);break; //红色
    default:Serial.println("NULL");break;
  }
  //多个气象预警显示，有空了再更新
  //if(Warn_Flag == 1) {
    if(dataWarnjson1["w5"].as<String>() != "null") {
      clk.loadFont(ZdyLwFont_20);
      clk.createSprite(90, 24); 
      clk.fillSprite(bgColor);
      clk.fillRoundRect(0,0,90,24,5,weatherWarnBgColor1);
      clk.setTextDatum(CC_DATUM);
      clk.setTextColor(TFT_WHITE); 
      clk.drawString(dataWarnjson1["w5"].as<String>(),45,14);
      //clk.drawString("预 警",45,45);
      clk.pushSprite(145,140);
      clk.deleteSprite();
      clk.unloadFont();
      clk.unloadFont();
    }
}

/*
<select id="kind" name="kind">
        <option value="" class="talarm">请选择预警种类</option>
        <option value="01">台风预警</option>
        <option value="02">暴雨预警</option>
        <option value="03">暴雪预警</option>
        <option value="04">寒潮预警</option>
        <option value="05">大风预警</option>
        <option value="06">沙尘暴预警</option>
        <option value="07">高温预警</option>
        <option value="08">干旱预警</option>
        <option value="09">雷电预警</option>
        <option value="10">冰雹预警</option>
        <option value="11">霜冻预警</option>
        <option value="12">大雾预警</option>
        <option value="13">霾预警</option>
        <option value="14">道路结冰预警</option>

        <option value="51">海上大雾预警</option>
        <option value="52">雷暴大风预警</option>
        <option value="53">持续低温预警</option>
        <option value="54">浓浮尘预警</option>
        <option value="55">龙卷风预警</option>
        <option value="56">低温冻害预警</option>
        <option value="57">海上大风预警</option>
        <option value="58">低温雨雪冰冻预警</option>
        <option value="59">强对流预警</option>
        <option value="60">臭氧预警</option>
        <option value="61">大雪预警</option>
        <option value="62">强降雨预警</option>
        <option value="63">强降温预警</option>
        <option value="64">雪灾预警</option>
        <option value="65">森林（草原）火险预警</option>
        <option value="66">雷暴预警</option>
        <option value="67">严寒预警</option>
        <option value="68">沙尘预警</option>
        <option value="69">海上雷雨大风预警</option>
        <option value="70">海上雷电预警</option>
        <option value="71">海上台风预警</option>
        <option value="72">低温预警</option>


        <option value="91">寒冷预警</option>
        <option value="92">灰霾预警</option>
        <option value="93">雷雨大风预警</option>
        <option value="94">森林火险预警</option>
        <option value="95">降温预警</option>
        <option value="96">道路冰雪预警</option>
        <option value="97">干热风预警</option>
        <option value="98">空气重污染预警</option>
        <option value="99">冰冻预警</option>

      </select>
      <select id="grade" name="grade">
        <option value="" class="calarm">请选择预警等级</option>
        <option value="04">红色</option>
        <option value="03">橙色</option>
        <option value="02">黄色</option>
        <option value="01">蓝色</option>
        <option value="05">未知</option>
      </select>
*/

int currentIndex = 0;
//int prevTime = 0;
TFT_eSprite clkb = TFT_eSprite(&tft);

void scrollBanner(){
  if(millis() - prevTime > 3000){ //3秒切换一次

    if(scrollText[currentIndex]){
  
      clkb.setColorDepth(8);
      clkb.loadFont(ZdyLwFont_20);
      
      for(int pos = 20; pos>0 ; pos--){
        scrollTxt(pos);
      }
      
      clkb.deleteSprite();
      clkb.unloadFont();
  
      if(currentIndex>=5){
        currentIndex = 0;  //回第一个
      }else{
        currentIndex += 1;  //准备切换到下一个  
      }
      
      //Serial.println(currentIndex);
      
    }
    prevTime = millis();

  }
}

void scrollTxt(int pos){
    clkb.createSprite(148, 24); 
    clkb.fillSprite(bgColor);
    clkb.setTextWrap(false);
    clkb.setTextDatum(CC_DATUM);
    clkb.setTextColor(TFT_BLACK, bgColor); 
    clkb.drawString(scrollText[currentIndex],74,pos+14);
    clkb.pushSprite(2,4);
}

/**
*底部生活信息滚动显示 
*/

byte ButtoncurrentIndex = 0;
unsigned long ButtonprevTime = 0;
TFT_eSprite clkbb = TFT_eSprite(&tft);

void ButtonscrollBanner(){
  if(millis() - ButtonprevTime > 5000){ //5秒切换一次

    if(ButtonScrollText[ButtoncurrentIndex]){
      clkbb.loadFont(ZdyLwFont_20);
      
      for(int pos = 20; pos>0 ; pos--){
        ButtonScrollTxt(pos);
      }
      
      clkbb.deleteSprite();
      clkbb.unloadFont();
  
      if(ButtoncurrentIndex>=7){
        ButtoncurrentIndex = 0;  //回第一个
      }else{
        ButtoncurrentIndex += 1;  //准备切换到下一个  
      }
      
      //Serial.println(ButtoncurrentIndex);
      
    }
    ButtonprevTime = millis();

  }
}

void ButtonScrollTxt(int pos){
  //clkbb.loadFont(ZdyLwFont_20);
  clkbb.createSprite(240, 40); 
  clkbb.fillSprite(bgColor);
  clkbb.setTextDatum(CC_DATUM);
  clkbb.setTextColor(TFT_BLACK, bgColor); 
  clkbb.drawString(ButtonScrollText[ButtoncurrentIndex],120,pos+20);
  clkbb.pushSprite(0,201);
  //clkbb.deleteSprite();
  //clkbb.unloadFont(); //释放加载字体资源
}

/*
void imgAnim(){
  int x=80,y=94,dt=30;//瘦子版dt=10毫秒 胖子30较为合适

  TJpgDec.drawJpg(x,y,i0, sizeof(i0));
  delay(dt);
  TJpgDec.drawJpg(x,y,i1, sizeof(i1));
  delay(dt);
  TJpgDec.drawJpg(x,y,i2, sizeof(i2));
  delay(dt);
  TJpgDec.drawJpg(x,y,i3, sizeof(i3));
  delay(dt);  
  TJpgDec.drawJpg(x,y,i4, sizeof(i4));
  delay(dt);  
  TJpgDec.drawJpg(x,y,i5, sizeof(i5));
  delay(dt);  
  TJpgDec.drawJpg(x,y,i6, sizeof(i6));
  delay(dt);  
  TJpgDec.drawJpg(x,y,i7, sizeof(i7));
  delay(dt);  
  TJpgDec.drawJpg(x,y,i8, sizeof(i8));
  delay(dt);  
  TJpgDec.drawJpg(x,y,i9, sizeof(i9));
  delay(dt);  

}
*/
/*
int Anim = 0;
int AprevTime = 0;
void imgAnim()
{
  int x=80,y=94;
  if(millis() - AprevTime > 40) //x ms切换一次
  {
    Anim++;
    AprevTime = millis();
  }
  if(Anim==10)
    Anim=0;

  switch(Anim)
  {
    case 0:
      TJpgDec.drawJpg(x,y,i0, sizeof(i0));
      break;
    case 1:
      TJpgDec.drawJpg(x,y,i1, sizeof(i1));
      break;
    case 2:
      TJpgDec.drawJpg(x,y,i2, sizeof(i2));
      break;
    case 3:
      TJpgDec.drawJpg(x,y,i3, sizeof(i3));
      break;
    case 4:
      TJpgDec.drawJpg(x,y,i4, sizeof(i4));
      break;
    case 5:
      TJpgDec.drawJpg(x,y,i5, sizeof(i5));
      break;
    case 6:
      TJpgDec.drawJpg(x,y,i6, sizeof(i6));
      break;
    case 7:
      TJpgDec.drawJpg(x,y,i7, sizeof(i7));
      break;
    case 8: 
      TJpgDec.drawJpg(x,y,i8, sizeof(i8));
      break;
    case 9: 
      TJpgDec.drawJpg(x,y,i9, sizeof(i9));
      break;
    default:
      Serial.println("显示Anim错误");
      break;
  }
}*/

//unsigned int Gif_Mode = 1;
unsigned long oldTime = 0,imgNum = 1;
void imgDisplay(){
  int x,y=94,dt;
  switch(Warn_Flag) {//如果有气象预警信息，图标自动左移
    case 0:x = 70;break;
    case 1:
      if(Gif_Mode == 4) {
        x = 65;y = 85;
      } 
      if(Gif_Mode == 3) {
        x = 65;y = 85;
      } 
     /* else if(Gif_Mode == 2) {
        x = 80;y = 94;
      }*/
      /*else if(Gif_Mode == 2) {
        x = 70;y = 86;
      }*/
      else if(Gif_Mode == 1) {
        x = 60;y = 94;
      }
      break;
  }
  switch(Gif_Mode) { //修改动画的播放速度
    case 1:dt = 100;break;
    case 2:dt = 50;break;
    case 3:dt = 100;break;
    case 4:dt = 100;break;
  }
  if(millis() - oldTime >= dt) {
    imgNum = imgNum + 1;
    oldTime = millis();
  }
  if(Gif_Mode == 1) { //动画-龙猫转圈
    y = 90;
    switch(imgNum) {
      case 1: TJpgDec.drawJpg(x,y,img_0, sizeof(img_0));break;
      case 2: TJpgDec.drawJpg(x,y,img_1, sizeof(img_1));break;
      case 3: TJpgDec.drawJpg(x,y,img_2, sizeof(img_2));break;
      case 4: TJpgDec.drawJpg(x,y,img_3, sizeof(img_3));break;
      case 5: TJpgDec.drawJpg(x,y,img_4, sizeof(img_4));break;
      case 6: TJpgDec.drawJpg(x,y,img_5, sizeof(img_5));break;
      case 7: TJpgDec.drawJpg(x,y,img_6, sizeof(img_6));break;
      case 8: TJpgDec.drawJpg(x,y,img_7, sizeof(img_7));break;
      case 9: TJpgDec.drawJpg(x,y,img_8, sizeof(img_8));break;
      case 10: TJpgDec.drawJpg(x,y,img_9, sizeof(img_9));break;
      case 11: TJpgDec.drawJpg(x,y,img_10, sizeof(img_10));break;
      case 12: TJpgDec.drawJpg(x,y,img_11, sizeof(img_11));break;
      case 13: TJpgDec.drawJpg(x,y,img_12, sizeof(img_12));break;
      case 14: TJpgDec.drawJpg(x,y,img_13, sizeof(img_13));break;
      case 15: TJpgDec.drawJpg(x,y,img_14, sizeof(img_14));break;
      case 16: TJpgDec.drawJpg(x,y,img_15, sizeof(img_15));break;
      case 17: TJpgDec.drawJpg(x,y,img_16, sizeof(img_16));break;
      case 18: TJpgDec.drawJpg(x,y,img_17, sizeof(img_17));break;
      case 19: TJpgDec.drawJpg(x,y,img_18, sizeof(img_18));break;
      case 20: TJpgDec.drawJpg(x,y,img_19, sizeof(img_19));break;
      case 21: TJpgDec.drawJpg(x,y,img_20, sizeof(img_20));break;
      case 22: TJpgDec.drawJpg(x,y,img_21, sizeof(img_21));break;
      case 23: TJpgDec.drawJpg(x,y,img_22, sizeof(img_22));break;
      case 24: TJpgDec.drawJpg(x,y,img_23, sizeof(img_23));break;
      case 25: TJpgDec.drawJpg(x,y,img_24, sizeof(img_24));break;
      case 26: TJpgDec.drawJpg(x,y,img_25, sizeof(img_25));break;
      case 27: TJpgDec.drawJpg(x,y,img_26, sizeof(img_26));break;
      case 28: TJpgDec.drawJpg(x,y,img_27, sizeof(img_27));break;
      case 29: TJpgDec.drawJpg(x,y,img_28, sizeof(img_28));break;
      case 30: TJpgDec.drawJpg(x,y,img_29, sizeof(img_29));break;
      case 31: TJpgDec.drawJpg(x,y,img_30, sizeof(img_30));break;
      case 32: TJpgDec.drawJpg(x,y,img_31, sizeof(img_31));break;
      case 33: TJpgDec.drawJpg(x,y,img_32, sizeof(img_32));break;
      case 34: TJpgDec.drawJpg(x,y,img_33, sizeof(img_33));break;
      case 35: TJpgDec.drawJpg(x,y,img_34, sizeof(img_34));break;
      case 36: TJpgDec.drawJpg(x,y,img_35, sizeof(img_35));break;
      case 37: TJpgDec.drawJpg(x,y,img_36, sizeof(img_36));break;
      case 38: TJpgDec.drawJpg(x,y,img_37, sizeof(img_37));break;
      case 39: TJpgDec.drawJpg(x,y,img_38, sizeof(img_38));break;
      case 40: TJpgDec.drawJpg(x,y,img_39, sizeof(img_39));break;
      case 41: TJpgDec.drawJpg(x,y,img_40, sizeof(img_40));break;
      case 42: TJpgDec.drawJpg(x,y,img_41, sizeof(img_41));break;
      case 43: TJpgDec.drawJpg(x,y,img_42, sizeof(img_42));break;
      case 44: TJpgDec.drawJpg(x,y,img_43, sizeof(img_43));break;
      case 45: TJpgDec.drawJpg(x,y,img_44, sizeof(img_44));break;
      case 46: TJpgDec.drawJpg(x,y,img_45, sizeof(img_45));break;
      case 47: TJpgDec.drawJpg(x,y,img_46, sizeof(img_46));break;
      case 48: TJpgDec.drawJpg(x,y,img_47, sizeof(img_47));break;
      case 49: TJpgDec.drawJpg(x,y,img_48, sizeof(img_48));break;
      case 50: TJpgDec.drawJpg(x,y,img_49, sizeof(img_49));break;
      case 51: TJpgDec.drawJpg(x,y,img_50, sizeof(img_50));break;
      case 52: TJpgDec.drawJpg(x,y,img_51, sizeof(img_51));break;
      case 53: TJpgDec.drawJpg(x,y,img_52, sizeof(img_52));break;
      case 54: TJpgDec.drawJpg(x,y,img_53, sizeof(img_53));break;
      case 55: TJpgDec.drawJpg(x,y,img_54, sizeof(img_54));break;
      case 56: TJpgDec.drawJpg(x,y,img_55, sizeof(img_55));break;
      case 57: TJpgDec.drawJpg(x,y,img_56, sizeof(img_56));break;
      case 58: TJpgDec.drawJpg(x,y,img_57, sizeof(img_57));break;
      case 59: TJpgDec.drawJpg(x,y,img_58, sizeof(img_58));break;
      case 60: TJpgDec.drawJpg(x,y,img_59, sizeof(img_59));break;
      case 61: TJpgDec.drawJpg(x,y,img_60, sizeof(img_60));break;
      case 62: TJpgDec.drawJpg(x,y,img_61, sizeof(img_61));break;
      case 63: TJpgDec.drawJpg(x,y,img_62, sizeof(img_62));break;
      case 64: TJpgDec.drawJpg(x,y,img_63, sizeof(img_63));break;
      case 65: TJpgDec.drawJpg(x,y,img_64, sizeof(img_64));break;
      case 66: TJpgDec.drawJpg(x,y,img_65, sizeof(img_65));break;
      case 67: TJpgDec.drawJpg(x,y,img_66, sizeof(img_66));break;
      case 68: TJpgDec.drawJpg(x,y,img_67, sizeof(img_67));break;
      case 69: TJpgDec.drawJpg(x,y,img_68, sizeof(img_68));break;
      case 70: TJpgDec.drawJpg(x,y,img_69, sizeof(img_69));break;
      case 71: TJpgDec.drawJpg(x,y,img_70, sizeof(img_70));break;
      case 72: TJpgDec.drawJpg(x,y,img_71, sizeof(img_71));break;
      case 73: TJpgDec.drawJpg(x,y,img_72, sizeof(img_72));break;
      case 74: TJpgDec.drawJpg(x,y,img_73, sizeof(img_73));break;
      case 75: TJpgDec.drawJpg(x,y,img_74, sizeof(img_74));break;
      case 76: TJpgDec.drawJpg(x,y,img_75, sizeof(img_75));break;
      case 77: TJpgDec.drawJpg(x,y,img_76, sizeof(img_76));break;
      case 78: TJpgDec.drawJpg(x,y,img_77, sizeof(img_77));break;
      case 79: TJpgDec.drawJpg(x,y,img_78, sizeof(img_78));break;
      case 80: TJpgDec.drawJpg(x,y,img_79, sizeof(img_79));imgNum=1;break;
    }
  }
  /*else if(Gif_Mode == 2) { //动画-太空人
    switch(imgNum) {
        case 1: TJpgDec.drawJpg(x,y,i0, sizeof(i0));break;
        case 2: TJpgDec.drawJpg(x,y,i1, sizeof(i1));break;
        case 3: TJpgDec.drawJpg(x,y,i2, sizeof(i2));break;
        case 4: TJpgDec.drawJpg(x,y,i3, sizeof(i3));break;
        case 5: TJpgDec.drawJpg(x,y,i4, sizeof(i4));break;
        case 6: TJpgDec.drawJpg(x,y,i5, sizeof(i5));break;
        case 7: TJpgDec.drawJpg(x,y,i6, sizeof(i6));break;
        case 8: TJpgDec.drawJpg(x,y,i7, sizeof(i7));break;
        case 9: TJpgDec.drawJpg(x,y,i8, sizeof(i8));break;
        case 10: TJpgDec.drawJpg(x,y,i9, sizeof(i9));imgNum=1;break;
      }
  }*/
  else if(Gif_Mode == 2) { //动画-龙猫睡觉
    y = 85;
    switch(imgNum) {
      case 1: TJpgDec.drawJpg(x,y,zzzzzzz_01_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 2: TJpgDec.drawJpg(x,y,zzzzzzz_02_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 3: TJpgDec.drawJpg(x,y,zzzzzzz_03_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 4: TJpgDec.drawJpg(x,y,zzzzzzz_04_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 5: TJpgDec.drawJpg(x,y,zzzzzzz_05_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 6: TJpgDec.drawJpg(x,y,zzzzzzz_06_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 7: TJpgDec.drawJpg(x,y,zzzzzzz_07_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 8: TJpgDec.drawJpg(x,y,zzzzzzz_08_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 9: TJpgDec.drawJpg(x,y,zzzzzzz_09_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 10: TJpgDec.drawJpg(x,y,zzzzzzz_10_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 11: TJpgDec.drawJpg(x,y,zzzzzzz_11_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 12: TJpgDec.drawJpg(x,y,zzzzzzz_12_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 13: TJpgDec.drawJpg(x,y,zzzzzzz_13_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 14: TJpgDec.drawJpg(x,y,zzzzzzz_14_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 15: TJpgDec.drawJpg(x,y,zzzzzzz_15_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 16: TJpgDec.drawJpg(x,y,zzzzzzz_16_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 17: TJpgDec.drawJpg(x,y,zzzzzzz_17_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 18: TJpgDec.drawJpg(x,y,zzzzzzz_18_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 19: TJpgDec.drawJpg(x,y,zzzzzzz_19_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 20: TJpgDec.drawJpg(x,y,zzzzzzz_20_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 21: TJpgDec.drawJpg(x,y,zzzzzzz_21_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 22: TJpgDec.drawJpg(x,y,zzzzzzz_22_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 23: TJpgDec.drawJpg(x,y,zzzzzzz_23_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 24: TJpgDec.drawJpg(x,y,zzzzzzz_24_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 25: TJpgDec.drawJpg(x,y,zzzzzzz_25_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 26: TJpgDec.drawJpg(x,y,zzzzzzz_26_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 27: TJpgDec.drawJpg(x,y,zzzzzzz_27_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 28: TJpgDec.drawJpg(x,y,zzzzzzz_28_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 29: TJpgDec.drawJpg(x,y,zzzzzzz_29_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 30: TJpgDec.drawJpg(x,y,zzzzzzz_30_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 31: TJpgDec.drawJpg(x,y,zzzzzzz_31_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 33: TJpgDec.drawJpg(x,y,zzzzzzz_32_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 34: TJpgDec.drawJpg(x,y,zzzzzzz_33_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 35: TJpgDec.drawJpg(x,y,zzzzzzz_34_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 36: TJpgDec.drawJpg(x,y,zzzzzzz_35_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 37: TJpgDec.drawJpg(x,y,zzzzzzz_36_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 38: TJpgDec.drawJpg(x,y,zzzzzzz_37_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 39: TJpgDec.drawJpg(x,y,zzzzzzz_38_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 40: TJpgDec.drawJpg(x,y,zzzzzzz_39_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 41: TJpgDec.drawJpg(x,y,zzzzzzz_40_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 42: TJpgDec.drawJpg(x,y,zzzzzzz_41_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 43: TJpgDec.drawJpg(x,y,zzzzzzz_42_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 44: TJpgDec.drawJpg(x,y,zzzzzzz_43_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 45: TJpgDec.drawJpg(x,y,zzzzzzz_45_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 46: TJpgDec.drawJpg(x,y,zzzzzzz_46_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 47: TJpgDec.drawJpg(x,y,zzzzzzz_47_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 48: TJpgDec.drawJpg(x,y,zzzzzzz_48_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 49: TJpgDec.drawJpg(x,y,zzzzzzz_49_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 50: TJpgDec.drawJpg(x,y,zzzzzzz_50_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 51: TJpgDec.drawJpg(x,y,zzzzzzz_51_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 52: TJpgDec.drawJpg(x,y,zzzzzzz_52_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 53: TJpgDec.drawJpg(x,y,zzzzzzz_53_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 54: TJpgDec.drawJpg(x,y,zzzzzzz_54_71x80, sizeof(zzzzzzz_01_71x80));break;
      case 55: TJpgDec.drawJpg(x,y,zzzzzzz_55_71x80, sizeof(zzzzzzz_55_71x80));imgNum=0;break;
      //case 56: TJpgDec.drawJpg(x,y,zzzzzzz_56_71x80, sizeof(zzzzzzz_56_71x80));imgNum=0;break;
    }
  }
    else if(Gif_Mode == 3) { //动画-龙猫跳绳
    y = 86;
    switch(imgNum) {
      case 1: TJpgDec.drawJpg(x,y,quan_0, sizeof(quan_0));break;
      case 2: TJpgDec.drawJpg(x,y,quan_1, sizeof(quan_1));break;
      case 3: TJpgDec.drawJpg(x,y,quan_2, sizeof(quan_2));break;
      case 4: TJpgDec.drawJpg(x,y,quan_3, sizeof(quan_3));break;
      case 5: TJpgDec.drawJpg(x,y,quan_4, sizeof(quan_4));break;
      case 6: TJpgDec.drawJpg(x,y,quan_5, sizeof(quan_5));break;
      case 7: TJpgDec.drawJpg(x,y,quan_6, sizeof(quan_6));break;
      case 8: TJpgDec.drawJpg(x,y,quan_7, sizeof(quan_7));break;
      case 9: TJpgDec.drawJpg(x,y,quan_8, sizeof(quan_8));break;
      case 10: TJpgDec.drawJpg(x,y,quan_9, sizeof(quan_9));break;
      case 11: TJpgDec.drawJpg(x,y,quan_10, sizeof(quan_10));break;
      case 12: TJpgDec.drawJpg(x,y,quan_11, sizeof(quan_11));break;
      case 13: TJpgDec.drawJpg(x,y,quan_12, sizeof(quan_12));break;
      case 14: TJpgDec.drawJpg(x,y,quan_13, sizeof(quan_13));break;
      case 15: TJpgDec.drawJpg(x,y,quan_14, sizeof(quan_14));break;
      case 16: TJpgDec.drawJpg(x,y,quan_15, sizeof(quan_15));break;
      case 17: TJpgDec.drawJpg(x,y,quan_16, sizeof(quan_16));break;
      case 18: TJpgDec.drawJpg(x,y,quan_17, sizeof(quan_17));break;
      case 19: TJpgDec.drawJpg(x,y,quan_18, sizeof(quan_18));break;
      case 20: TJpgDec.drawJpg(x,y,quan_19, sizeof(quan_19));imgNum=1;break;
      /*case 21: TJpgDec.drawJpg(x,y,quan_20, sizeof(quan_20));break;
      case 22: TJpgDec.drawJpg(x,y,quan_21, sizeof(quan_21));break;
      case 23: TJpgDec.drawJpg(x,y,quan_22, sizeof(quan_22));break;
      case 24: TJpgDec.drawJpg(x,y,quan_23, sizeof(quan_23));break;
      case 25: TJpgDec.drawJpg(x,y,quan_24, sizeof(quan_24));break;
      case 26: TJpgDec.drawJpg(x,y,quan_25, sizeof(quan_25));break;
      case 27: TJpgDec.drawJpg(x,y,quan_26, sizeof(quan_26));break;
      case 28: TJpgDec.drawJpg(x,y,quan_27, sizeof(quan_27));break;
      case 29: TJpgDec.drawJpg(x,y,quan_28, sizeof(quan_28));break;
      case 30: TJpgDec.drawJpg(x,y,quan_29, sizeof(quan_29));break;
      case 31: TJpgDec.drawJpg(x,y,quan_30, sizeof(quan_30));break;
      case 32: TJpgDec.drawJpg(x,y,quan_31, sizeof(quan_31));break;
      case 33: TJpgDec.drawJpg(x,y,quan_32, sizeof(quan_32));break;
      case 34: TJpgDec.drawJpg(x,y,quan_33, sizeof(quan_33));break;
      case 35: TJpgDec.drawJpg(x,y,quan_34, sizeof(quan_34));break;
      case 36: TJpgDec.drawJpg(x,y,quan_35, sizeof(quan_35));break;
      case 37: TJpgDec.drawJpg(x,y,quan_36, sizeof(quan_36));break;
      case 38: TJpgDec.drawJpg(x,y,quan_37, sizeof(quan_37));break;
      case 39: TJpgDec.drawJpg(x,y,quan_38, sizeof(quan_38));break;
      case 40: TJpgDec.drawJpg(x,y,quan_39, sizeof(quan_39));break;imgNum=1;break;*/
    }
  }
}

void digitalClockDisplay()
{
  
  clk.setColorDepth(8);

  /***中间时间区***/
  //时分
  clk.createSprite(140, 48);
  clk.fillSprite(bgColor);
  //clk.loadFont(FxLED_48);
  clk.setTextDatum(CC_DATUM);
  clk.setTextColor(TFT_BLACK, bgColor);
  clk.drawString(hourMinute(),70,24,7); //绘制时和分
  //clk.unloadFont();
  clk.pushSprite(28,40);
  clk.deleteSprite();
  
  //秒
  clk.createSprite(40, 32);
  clk.fillSprite(bgColor);
  
  clk.loadFont(FxLED_32);
  clk.setTextDatum(CC_DATUM);
  clk.setTextColor(TFT_BLACK, bgColor); 
  clk.drawString(num2str(second()),20,16);
  
  clk.unloadFont();
  clk.pushSprite(170,60);
  clk.deleteSprite();
  /***中间时间区***/

  /***底部***/
  clk.loadFont(ZdyLwFont_20);
  clk.createSprite(58, 32);
  clk.fillSprite(bgColor);

  //星期
  clk.setTextDatum(CC_DATUM);
  clk.setTextColor(TFT_BLACK, bgColor);
  clk.drawString(week(),29,16);
  clk.pushSprite(1,168);
  clk.deleteSprite();
  
  //月日
  clk.createSprite(98, 32);
  clk.fillSprite(bgColor);
  clk.setTextDatum(CC_DATUM);
  clk.setTextColor(TFT_BLACK, bgColor);  
  clk.drawString(monthDay(),49,16);
  clk.pushSprite(61,168);
  clk.deleteSprite();
  
  
  clk.unloadFont();
  /***底部***/
}

//星期
String week(){
  String wk[7] = {"日","一","二","三","四","五","六"};
  String s = "周" + wk[weekday()-1];
  return s;
}

//月日
String monthDay(){
  String s = String(month());
  s = s + "月" + day() + "日";
  return s;
}
//时分
String hourMinute(){
  String s = num2str(hour());
  s = s + ":" + num2str(minute());
  return s;
}

String num2str(int number){
  if (number >= 10){
    return String(number);
  } else {
    return String("0" + String(number));
  }
}
/*
String num2str(int digits)
{
  String s = "";
  if (digits < 10)
    s = s + "0";
  s = s + digits;
  return s;
}*/

void printDigits(int digits)
{
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

/*-------- NTP code ----------*/

const int NTP_PACKET_SIZE = 48; // NTP时间在消息的前48字节中
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime()
{
  IPAddress ntpServerIP; // NTP server's ip address

  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  //Serial.println("Transmit NTP Request");
  // get a random server from the pool
  WiFi.hostByName(ntpServerName, ntpServerIP);
  //Serial.print(ntpServerName);
  //Serial.print(": ");
  //Serial.println(ntpServerIP);
  sendNTPpacket(ntpServerIP);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 1500) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      Serial.println("Receive NTP Response");
      Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      //Serial.println(secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR);
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  Serial.println("No NTP Response :-(");
  return 0; // 无法获取时间时返回0
}

// 向NTP服务器发送请求
void sendNTPpacket(IPAddress &address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12] = 49;
  packetBuffer[13] = 0x4E;
  packetBuffer[14] = 49;
  packetBuffer[15] = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}
